package edu.westga.sketchit2009cs.tester;

import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;

import org.junit.Before;
import org.junit.Test;

import edu.westga.sketchit2009cs.fakeNetwork.FakeNetwork;

/**
 * This tester tests the FakeNetwork class, which saves data
 * to flat files.
 * @author RJ Hill
 *
 */
public class FakeNetworkTester {
	private FakeNetwork theNetwork;
	private File theFile;
	private BufferedReader output;
	
	private Calendar rightNow = Calendar.getInstance();
	private String thisDay = Integer.toString(rightNow.get(Calendar.DAY_OF_MONTH));
	private String thisMonth = Integer.toString(rightNow.get(Calendar.MONTH));
	private String thisYear = Integer.toString(rightNow.get(Calendar.YEAR));
	private String datePrefix = this.thisDay + this.thisMonth + this.thisYear;
	
	private String connectData = "connected";
	private String sendData = "sending";
	private String recieveData = "recieving";
	
	@Before
	public void SetUp() {
		this.theNetwork = new FakeNetwork();
	}
	
	@Test
	public void shouldGetConnectDataWhenReadingConnectionLogAfterConnectToNetwork() {
		this.theNetwork.connectToNetwork(connectData);
		String fileName = "connectLog" + datePrefix + ".txt";
		assertEquals(readFromFile(fileName).trim(), connectData);
	}
	
	@Test
	public void shouldGetSendDataWhenReadingSendLogAfterSendData() {
		this.theNetwork.sendData(sendData);
		String fileName = "sendLog" + datePrefix + ".txt";
		assertEquals(readFromFile(fileName).trim(), sendData);
	}
	
	@Test
	public void shouldGetRecieveDataWhenReadingRecieveLogAfterRecieveData() {
		this.theNetwork.receiveData(recieveData);
		String fileName = "recieveLog" + datePrefix + ".txt";
		assertEquals(readFromFile(fileName).trim(), recieveData);
	}
	
	/**
	 * Reads data from a file, imports it and makes it into a string.
	 * <p>
	 * Requires: theFile != null
	 * <p>
	 * Ensures: contents != null
	 * @param fileName the name of the file with extension and path
	 * @return
	 */
	public String readFromFile(String fileName) {
		this.theFile = new File(fileName);
		
		//read each line into a string
		StringBuilder contents = new StringBuilder();
	    
	    try {
	    	
	    	//use buffering, reading one line at a time
	    	//FileReader always assumes default encoding is OK!
	    	output =  new BufferedReader(new FileReader(this.theFile));
	    	try {
	    		String line = null; //not declared within while loop

	    		//returns the content of a line MINUS the newline.
	    		while (( line = output.readLine()) != null) {
	    			contents.append(line);
	    			contents.append(System.getProperty("line.separator"));
	    		}
	    	}
	    	finally {
	    		output.close();
	    	}
	    }
	    catch (IOException ex){
	    	ex.printStackTrace();
	    }
	    return contents.toString(); 
	}
	
}
